package com.hp.order_service.controller;

import com.hp.order_service.entity.Goods;
import com.hp.order_service.entity.Order;
import com.hp.order_service.feign.GoodsFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
public class OrderController {

    @Value("${spring.datasource.url}")
    private String url;

    public static final List<Order> ORDER_LIST = Arrays.asList(
            new Order(1L,"zhangsan",1L),
            new Order(2L,"lisi",3L),
            new Order(3L,"zhangsan",4L),
            new Order(4L,"zhangsan",2L)
    );

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @RequestMapping("/order/{id}")
    public ResponseEntity<Order> getOrderById(@PathVariable Long id){
        System.out.println(url);
        Order order = null;
        for (Order o : ORDER_LIST){
            if(o.getId() == id){
                order = o;
            }
        }
        if(order != null){
            //查询商品
            ResponseEntity<Goods> entity = goodsFeignClient.getGoodsById(order.getGoodsId());
            order.setGoods(entity.getBody());
        }
        return ResponseEntity.ok(order);
    }

    @RequestMapping("/orders")
    public ResponseEntity<List<Order>> getAllOrders(){
        for(Order order : ORDER_LIST){
            ResponseEntity<Goods> entity = goodsFeignClient.getGoodsById(order.getGoodsId());
            order.setGoods(entity.getBody());
        }
        return ResponseEntity.ok(ORDER_LIST);
    }

}
